﻿Public Class CLabelCheckState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABELCHECK", "CHNG") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Dim webMng As CWebManager = CWebManager.GetInstance
        Dim checkInfo As CCheck = CCheck.GetIns
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance

        If task.ProcessText = "" Then
            Dim label1 As String = ""
            Dim statusMsg As String
            If pdaTask.ChkMazdaLabel = "" Then
                label1 += CConst.CNS_MSG_017
            End If

            If pdaTask.ChkDNLabel = "" Then
                label1 += CConst.CNS_MSG_018
            End If

            If pdaTask.ChkMazdaLabel = "" And pdaTask.ChkDNLabel = "" Then
                statusMsg = CConst.CNS_MSG_012
            Else
                statusMsg = CCommon.msgFormat(CConst.CNS_MSG_016, label1)
            End If

            '' リーダーからデータを取得する(待ち時間制限なし)
            pdaTask.WindowManager.SetMessage(statusMsg, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
            task.ProcessText = task.ReadTextWait(-1)
            Debug.WriteLine("読んだバーコード:" & task.ProcessText)
        End If
        Debug.WriteLine("CLabelCheckState...START")

        Dim GetBarcodeVl As String = CBarcode.getBarcodeVl(task.ProcessText, CBarcode.GetBarcodeType(task.ProcessText))

        Select Case CBarcode.GetBarcodeType(task.ProcessText)


            '取消処理
            Case EnumBarcodeType.CANCEL_CODE
                If pdaTask.ChkDNLabel = "" And pdaTask.ChkMazdaLabel = "" Then

                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                        Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_025, Color.White)

                    task.ProcessText = ""
                    task.ActionCd = "LABELCHECK"

                Else
                    Dim sendFlg As Boolean = CPersonal.ConfirmWindow(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                                Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_024, Color.White)
                    If sendFlg Then
                        If Not pdaTask.MazadaLabel = "" Then
                            pdaTask.ListCnt -= 1
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                            '画面にある納品番号を削除
                            CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                            pdaTask.WindowManager.SetRecordDelegate()
                            pdaTask.WindowManager.RefreshWindow(1)
                        End If

                        pdaTask.MazadaLabel = ""
                        pdaTask.ChkDNLabel = ""
                        pdaTask.ChkMazdaLabel = ""

                        'CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                        pdaTask.WindowManager.ClearActionLabel()

                        task.ProcessText = ""
                        task.ActionCd = "LABELCHECK"

                    Else
                        If Not pdaTask.MazadaLabel = "" Then
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                            Dim listCount As Integer = pdaTask.WindowManager.GetRecordCount()
                            For cn As Integer = 0 To listCount - 1
                                Dim setline As COrderRecord = pdaTask.WindowManager.GetRecord(cn)
                                'setline.RowColor = Color.White
                                If setline.InvoiceCd = setData.InvoiceCd Then
                                    setline.RowColor = System.Drawing.Color.GreenYellow
                                    Exit For
                                End If
                            Next
                        End If

                      
                    End If
                End If

                '途中送信
            Case EnumBarcodeType.SEND_CODE

                Dim sendFlg As Boolean = CPersonal.ConfirmWindow(pdaTask, EnumWindowMode.YESNO_MODE, _
                                                                Color.Black, CConst.EnumSound.YESNO_BUZZ, CConst.CNS_MSG_008, Color.White)
                If sendFlg Then
                    '送信データが存在チェック
                    If checkInfo.DataCountCheck("2") Then
                        Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                        CBgControl.BgThread("UNI", "csv", filePath, checkInfo.GetUnique_2PK_Data(pdaTask.PdaNo, "2"), CConst.ORDER_DATA_UPLOAD)
                        pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_009, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                    End If
                End If

                task.ProcessText = ""
                task.ActionCd = "LABELCHECK"

            Case EnumBarcodeType.END_LABEL_CODE
                '送信データが存在チェック
                If checkInfo.DataCountCheck("2") Then
                    Dim filePath As String = CApplicationManager.GetRealPath(CApplicationManager.SETTING_PATH, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                    CBgControl.BgThread("UNI", "csv", filePath, checkInfo.GetUnique_2PK_Data(pdaTask.PdaNo, "2"), CConst.ORDER_DATA_UPLOAD)
                    pdaTask.WindowManager.SetMessage(CConst.CNS_MSG_009, CConst.COLOR_BLACK, CConst.COLOR_GREEN)
                End If

                '終了コード
                task.ProcessText = ""
                pdaTask.WindowManager.CloseWindow() ''強制終了
                task.ActionCd = "LOUT"

                '交代
            Case EnumBarcodeType.EMP_CODE
                Dim EmpData As CCheck.EmpData = checkInfo.ChkEmpData(task.ProcessText)

                pdaTask.WindowManager.SetEmpInfo(EmpData.empCd & ":[" & EmpData.empName & "]")
                pdaTask.EmpCd = EmpData.empCd
                task.ActionCd = "LABELCHECK"

                'DN現品書
            Case EnumBarcodeType.DN_DELIVERY_CODE

                If chkLabel(pdaTask, GetBarcodeVl) Then
                    If pdaTask.ChkDNLabel = "" Then
                        pdaTask.ChkDNLabel = GetBarcodeVl
                        'ユニークコードチェック
                        If checkInfo.ChkUniqueCd_2PK_Data(GetBarcodeVl) Then
                            If pdaTask.MazadaLabel = "" Then
                                pdaTask.MazadaLabel = GetBarcodeVl

                                Dim record As COrderRecord = pdaTask.WindowManager.CreateOrderRecord
                                pdaTask.ListCnt += 1
                                With record
                                    .RowNum = pdaTask.ListCnt
                                    .InvoiceCd = pdaTask.MazadaLabel
                                    .IsEndFlg = ""
                                    .RowColor = System.Drawing.Color.GreenYellow
                                End With

                                '画面設定
                                CPersonal.AddWindowRecord(record, pdaTask.WindowManager)
                                pdaTask.WindowManager.SetRecordDelegate()
                                pdaTask.WindowManager.RefreshWindow(1)
                            End If

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)
                        Else
                            '重複するuniqueCdがある場合
                            Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkGetUniqueCd_2PK_Data(GetBarcodeVl)
                            Dim readYear As String = uniqueData.actionDateTime.Substring(2, 2)
                            Dim readMonth As String = uniqueData.actionDateTime.Substring(5, 2)
                            Dim readDay As String = uniqueData.actionDateTime.Substring(8, 2)
                            Dim readHour As String = uniqueData.actionDateTime.Substring(11, 2)
                            Dim readMin As String = uniqueData.actionDateTime.Substring(14, 2)

                            Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                            '統括コード
                            Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_015, readYear, readMonth, readDay, readHour, readMin, empInfo.empName)
                            ' 重複するuniqueCdがある場合
                            CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                                  Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)

                            If Not pdaTask.MazadaLabel = "" Then
                                pdaTask.ListCnt -= 1
                                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                                '画面にある納品番号を削除
                                CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                                pdaTask.WindowManager.SetRecordDelegate()
                                pdaTask.WindowManager.RefreshWindow(1)
                            End If

                            pdaTask.MazadaLabel = ""
                            pdaTask.ChkDNLabel = ""
                            pdaTask.ChkMazdaLabel = ""

                            'CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                            pdaTask.WindowManager.ClearActionLabel()

                            task.ActionCd = "LABELCHECK"
                        End If
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                        task.ActionCd = "LABELCHECK"

                        If Not pdaTask.MazadaLabel = "" Then
                            pdaTask.ListCnt -= 1
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                            '画面にある納品番号を削除
                            CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                            pdaTask.WindowManager.SetRecordDelegate()
                            pdaTask.WindowManager.RefreshWindow(1)
                        End If

                        pdaTask.ChkDNLabel = ""
                        pdaTask.ChkMazdaLabel = ""
                        pdaTask.MazadaLabel = ""

                        'CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                        pdaTask.WindowManager.ClearActionLabel()
                    End If
                Else
                    If Not pdaTask.MazadaLabel = "" Then
                        pdaTask.ListCnt -= 1
                    End If

                    pdaTask.ChkMazdaLabel = ""
                    pdaTask.ChkDNLabel = ""
                    pdaTask.MazadaLabel = ""

                    task.ActionCd = "LABELCHECK"
                    task.ProcessText = ""

                    'CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                    pdaTask.WindowManager.ClearActionLabel()
                End If

                'MAZADA納品書
            Case EnumBarcodeType.MAZDA_DELIVERY_CODE

                If chkLabel(pdaTask, GetBarcodeVl) Then
                    If pdaTask.ChkMazdaLabel = "" Then
                        pdaTask.ChkMazdaLabel = GetBarcodeVl
                        'ユニークコードチェック
                        If checkInfo.ChkUniqueCd_2PK_Data(GetBarcodeVl) Then
                            If pdaTask.ChkDNLabel = "" Then
                                pdaTask.MazadaLabel = GetBarcodeVl

                                Dim record As COrderRecord = pdaTask.WindowManager.CreateOrderRecord
                                pdaTask.ListCnt += 1
                                With record
                                    .RowNum = pdaTask.ListCnt
                                    .InvoiceCd = pdaTask.MazadaLabel
                                    .IsEndFlg = ""
                                    .RowColor = System.Drawing.Color.GreenYellow
                                End With

                                '画面設定
                                CPersonal.AddWindowRecord(record, pdaTask.WindowManager)
                                pdaTask.WindowManager.SetRecordDelegate()
                                pdaTask.WindowManager.RefreshWindow(1)
                            End If

                            CPersonal.addWindowLabelBox(CBarcode.GetBarcodeType(task.ProcessText), pdaTask.WindowManager)

                        Else
                            '重複するuniqueCdがある場合
                            Dim uniqueData As CCheck.UniqueCdData = checkInfo.ChkGetUniqueCd_2PK_Data(GetBarcodeVl)
                            Dim readYear As String = uniqueData.actionDateTime.Substring(2, 2)
                            Dim readMonth As String = uniqueData.actionDateTime.Substring(5, 2)
                            Dim readDay As String = uniqueData.actionDateTime.Substring(8, 2)
                            Dim readHour As String = uniqueData.actionDateTime.Substring(11, 2)
                            Dim readMin As String = uniqueData.actionDateTime.Substring(14, 2)

                            Dim empInfo As CCheck.EmpData = checkInfo.ChkEmpData(uniqueData.empCd)
                            '統括コード
                            Dim errMsg As String = CCommon.msgFormat(CConst.CNS_MSG_015, readYear, readMonth, readDay, readHour, readMin, empInfo.empName)
                            ' 重複するuniqueCdがある場合
                            CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                                  Color.Black, CConst.EnumSound.ALERT_BUZZ, errMsg, Color.White)
                            If Not pdaTask.MazadaLabel = "" Then
                                pdaTask.ListCnt -= 1
                                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                                '画面にある納品番号を削除
                                CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                                pdaTask.WindowManager.SetRecordDelegate()
                                pdaTask.WindowManager.RefreshWindow(1)
                            End If

                            pdaTask.ChkDNLabel = ""
                            pdaTask.ChkMazdaLabel = ""
                            pdaTask.MazadaLabel = ""
                            ' CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                            pdaTask.WindowManager.ClearActionLabel()

                            task.ActionCd = "LABELCHECK"
                        End If
                    Else
                        CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_007, Color.White)
                        task.ActionCd = "LABELCHECK"

                        If Not pdaTask.MazadaLabel = "" Then
                            pdaTask.ListCnt -= 1
                            Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                            '画面にある納品番号を削除
                            CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                            pdaTask.WindowManager.SetRecordDelegate()
                            pdaTask.WindowManager.RefreshWindow(1)
                        End If

                        pdaTask.ChkDNLabel = ""
                        pdaTask.ChkMazdaLabel = ""
                        pdaTask.MazadaLabel = ""
                        'CPersonal.setWindowLabelBox(pdaTask.WindowManager)
                        pdaTask.WindowManager.ClearActionLabel()

                    End If


                Else
                    If Not pdaTask.MazadaLabel = "" Then
                        pdaTask.ListCnt -= 1
                    End If

                    CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                      Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                    task.ActionCd = "LABELCHECK"

                    If Not pdaTask.MazadaLabel = "" Then
                        pdaTask.ListCnt -= 1
                        Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                        '画面にある納品番号を削除
                        CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                        pdaTask.WindowManager.SetRecordDelegate()
                        pdaTask.WindowManager.RefreshWindow(1)
                    End If

                    pdaTask.ChkDNLabel = ""
                    pdaTask.ChkMazdaLabel = ""
                    pdaTask.MazadaLabel = ""

                    'CPersonal.setWindowLabelBox(pdaTask.WindowManager)

                    pdaTask.WindowManager.ClearActionLabel()
                    task.ActionCd = "LABELCHECK"
                    task.ProcessText = ""

                End If

            Case Else

                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                  Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                task.ActionCd = "LABELCHECK"

                If Not pdaTask.MazadaLabel = "" Then
                    pdaTask.ListCnt -= 1
                    Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                    '画面にある納品番号を削除
                    CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                    pdaTask.WindowManager.SetRecordDelegate()
                    pdaTask.WindowManager.RefreshWindow(1)
                End If

                pdaTask.ChkDNLabel = ""
                pdaTask.ChkMazdaLabel = ""
                pdaTask.MazadaLabel = ""

                'CPersonal.setWindowLabelBox(pdaTask.WindowManager)

                pdaTask.WindowManager.ClearActionLabel()
                task.ActionCd = "LABELCHECK"
                task.ProcessText = ""

        End Select

        If pdaTask.ChkMazdaLabel = pdaTask.ChkDNLabel And Not pdaTask.ChkMazdaLabel = "" Then
            'INNER_STATEに行く
            task.ActionCd = "LABEL-IN"
        End If

        Debug.WriteLine("CLabelCheckState...END")
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState

    End Function


    Private Function chkLabel(ByVal task As CPDATask, ByVal label As String) As Boolean

        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        'チェック
        If Not pdaTask.MazadaLabel = "" Then
            If Not pdaTask.MazadaLabel = label Then
                CPersonal.ErrorWindow(pdaTask, EnumWindowMode.ALERT_MODE, _
                                                        Color.Black, CConst.EnumSound.ALERT_BUZZ, CConst.CNS_MSG_021, Color.White)
                Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentInvoice(pdaTask.MazadaLabel)
                '画面にある納品番号を削除
                CPersonal.DelWindowRecord(setData, pdaTask.WindowManager)

                pdaTask.WindowManager.SetRecordDelegate()
                pdaTask.WindowManager.RefreshWindow(1)

                Return False
            End If
        End If
        Return True

    End Function
    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.DoExit")
        Return True
    End Function

End Class
